<?php

require( "functions.php" );

$topNode = LoggerCreateXMLDocument();

if ( LoggerOpenDB() )
{
	if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "message_id" ], $_POST[ "message_time" ],
		$_POST[ "to_logger_id" ], $_POST[ "message_body" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$message_time = mysql_real_escape_string( rawurldecode( $_POST[ "message_time" ] ) );
		$to_logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "to_logger_id" ] ) );
		$message_body = mysql_real_escape_string( utf8_decode( rawurldecode( $_POST[ "message_body" ] ) ) );
		$message_id = mysql_real_escape_string( rawurldecode( $_POST[ "message_id" ] ) );
		$sql = "INSERT INTO messages ( net_id, logger_id, message_id, message_time, to_logger_id, message_body ) " .
			"VALUES ( $net_id, '$logger_id', '$message_id', '$message_time', '$to_logger_id', '$message_body' )";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
			$error = "Message insert failure";
	}
	else
	{
		$error = "Submission failure";
	}
}
else
{
	$error = "DB open failure";
}

if ( isset( $error ) )
{
	LoggerAppendError( $topNode, $error );
}

$doc = $topNode->ownerDocument;
echo $doc->saveXML();

?>